package com.sundi.ecmarket.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.config.ExcelBean;
import com.sundi.ecmarket.config.ExcelUtils;
import com.sundi.ecmarket.dto.IssueDto;
import com.sundi.ecmarket.entity.EcIssue;
import com.sundi.ecmarket.mapper.EcIssueMapper;
import com.sundi.ecmarket.service.IEcIssueService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * <p>
 * 常见问题表 服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@Service
public class EcIssueServiceImpl extends ServiceImpl<EcIssueMapper, EcIssue> implements IEcIssueService {

    @Override
    public List<EcIssue> selectIssueList(IssueDto issueDto) {
        return this.baseMapper.selectIssueList(issueDto);
    }

    @Override
    public void updateLogicDel(Integer id) {
        this.baseMapper.updateLogicDel(id);
    }


    @Override
    public EcIssue getStuXiaOrder(Integer id) {
        return this.baseMapper.getStuXiaOrder(id);
    }

    @Override
    public XSSFWorkbook downLoadGoodsExcel(IssueDto issueDto) throws Exception {
        PageHelper.startPage(issueDto.getPageNo(),issueDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();

        if (StringUtils.isNotBlank(issueDto.getQuestion())){
            wrapper.like("question",issueDto.getQuestion());
        }
        List<EcIssue> list = this.selectList(wrapper);
        PageInfo<EcIssue> pageInfo = new PageInfo(list);
        Map<Integer,List<ExcelBean>> map = new HashMap<Integer,List<ExcelBean>>();
        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("问题标题","question",0));
        excelBeans.add(new ExcelBean("问题答案","answer",0));
        map.put(0,excelBeans);

        return ExcelUtils.createExcelFile(EcIssue.class,pageInfo.getList(),map,"商铺列表");
    }


    @Override
    public void uplodExcel(MultipartFile multipartFile) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
        List<EcIssue> list = new ArrayList<>();

        for (List<Object> objects : bankListByExcel) {
            EcIssue ecIssue = new EcIssue();
            ecIssue.setQuestion(objects.get(1).toString());
            ecIssue.setAnswer(objects.get(2).toString());
            list.add(ecIssue);
        }
        this.insertBatch(list);
    }
}
